/* eslint-disable no-param-reassign */
import strategyApi from '@/api/modules/strategy'
const initQueryParams = {
  type: 1,
  name: '',
  createBy: undefined,
  vehicleVersionId: undefined
}
const initRightDrawerEvent = {
  // 触发的事件名
  key: null,
  // 触发事件所传递的数据
  data: null
}
const initTaskVehicleQueryParams = {
  taskId: ''
}
const state = {
  // 查询策略条件
  queryParams: { ...initQueryParams },
  // 查看测试中查询关联车辆搜索条件
  taskVehicleQueryParams: {
    ...initTaskVehicleQueryParams
  },
  // 右侧抽屉框事件(例如点击关联任务查看任务事件)
  rightDrawerEvent: { ...initRightDrawerEvent }
}
const mutations = {
  // 清空查询条件
  resetStrategyQueryParams (state) {
    state.queryParams = { ...initQueryParams, type: state.queryParams.type }
  },
  // 清空测试中查询关联车辆搜索条件
  resetStrategyTaskVehicleQueryParams (state) {
    state.taskVehicleQueryParams = { ...initTaskVehicleQueryParams }
  },
  setStrategyRightDrawerEvent (state, val) {
    state.rightDrawerEvent = val
  },
  resetStrategyRightDrawerEvent (state) {
    state.rightDrawerEvent = { ...initRightDrawerEvent }
  }
}
const actions = {
  // 添加
  // eslint-disable-next-line no-unused-vars
  addStrategyData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      strategyApi.addStrategyData(data, data.versionType, data.upgradeTriggerType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 编辑
  // eslint-disable-next-line no-unused-vars
  editStrategyData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      strategyApi.editStrategyData(data, data.versionType, data.upgradeTriggerType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 编辑
  // eslint-disable-next-line no-unused-vars
  deleteStrategyData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      strategyApi.deleteStrategyData(data.ids, data.versionType, data.upgradeTriggerType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取策略详情
  // eslint-disable-next-line no-unused-vars
  getStrategyDetailData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      strategyApi.getStrategyDetailById(query.id).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取创建策略的用户下拉数据
  // eslint-disable-next-line no-unused-vars
  getCreatedStrategyUsersData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      strategyApi.getCreateStrategyUsers(query, query.versionType, query.upgradeTriggerType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 发布策略数据
  // eslint-disable-next-line no-unused-vars
  publishStrategyData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      strategyApi.publishStrategy(data, data.versionType, data.upgradeTriggerType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 停用策略数据
  // eslint-disable-next-line no-unused-vars
  disabledStrategyData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      strategyApi.disabledStrategy(data, data.versionType, data.upgradeTriggerType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 添加sota条件/升级策略
  // eslint-disable-next-line no-unused-vars
  addSotaStrategyData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      strategyApi.addSotaStrategy(data).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 编辑sota条件/升级策略
  // eslint-disable-next-line no-unused-vars
  editSotaStrategyData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      strategyApi.editSotaStrategy(data).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
